#==========================================
# URL: https://github.com/wklken/k-tmux
# Author:  wklken
# Version: 0.1
# Email: wklken@yeah.net
# ReadMe: README.md
# Donation: http://www.wklken.me/pages/donation.html
# Last_modify: 2015-08-06
#==========================================
# Dependecny:
#    1. brew install reattach-to-user-namespace
#    2. gem install tmuxinator
#    3. vim: christoomey/vim-tmux-navigator


#--------------------------- base config begin -------------------------------

# 全局设置
setw -g xterm-keys on
set -g status-utf8 on                             # enable utf-8
set-option -g default-terminal "screen-256color"  # use 256 colors
set-option -g history-limit 100000                # scrollback buffer n lines
set-option -g display-time 5000                   # 提示信息的持续时间；设置足够的时间以避免看不清提示，单位为毫秒
set-option -g repeat-time 1000                    # 控制台激活后的持续时间；设置合适的时间以避免每次操作都要先激活控制台，单位为毫秒
set-window-option -g display-panes-time 1500
set-option -g repeat-time 0                       # Allow the arrow key to be used immediately after changing windows

set-option -g renumber-windows on                 # window 0, 1, 2, remove 1, renumber to 0, 1

# Set window notifications
setw -g monitor-activity on
set -g visual-activity on

# use C-a, since it's on the home row and easier to hit than C-b
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
# make `ctrl+a ctrl+a` to move to beginning of the line
bind a send-prefix

# bind a reload key: prefix+r
bind R source-file ~/.tmux.conf \; display-message "Config reloaded.."



# -------------------------------- session begin --------------------------------
bind -r ( switch-client -p
bind -r ) switch-client -n
# -------------------------------- session end --------------------------------

# -------------------------------- window begin --------------------------------
set-option -g base-index 1                        #窗口的初始序号；默认为0，这里设置为1

set-window-option -g pane-base-index 1            # panes index start at 1, setw means set-window-option
set-window-option -g other-pane-height 25
set-window-option -g other-pane-width 80

# [prefix-c] 创建新窗口
bind-key c new-window
# [prefix-数字] 切换窗口
# [prefix-p] 切换窗口

# [prefix-t/T] 切换窗口
bind-key t next-window
bind-key T previous-window
# bind-key space next-window
# bind-key bspace previous-window

# [prefix-Ctrl-o] 窗口轮转
bind-key C-o rotate-window


# -------------------------------- window end --------------------------------

# -------------------------------- pane begin --------------------------------
# use vim-like keys for splits and windows
# v / s 竖直、水平分割window为pane
unbind '"'
unbind %
# 窗口切割  split windows like vim.  - Note: vim's definition of a horizontal/vertical split is reversed from tmux's
# [prefix-v] / [prefix-s] 垂直/水平分割窗口
# bind-key v split-window -h
# bind-key s split-window -v

# key bindings for horizontal and vertical panes
# 键 [\|] 和 [_-] 少敲一个shift键
bind-key \ split-window -h
bind-key - split-window -v

# 窗口间切换
# [prefix-hjkl] pane之间移动
unbind-key h
bind-key h select-pane -L
unbind-key j
bind-key j select-pane -D
unbind-key k
bind-key k select-pane -U
unbind-key l
bind-key l select-pane -R
# smart pane switching with awareness of vim splits
# [Ctrl-hjkl] pane之间移动
bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-h) || tmux select-pane -L"
bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-j) || tmux select-pane -D"
bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-k) || tmux select-pane -U"
bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys C-l) || tmux select-pane -R"
# [Ctrl-l] 左右两个切  [Ctrl-\] 最近的两个切换
bind -n C-\ run "(tmux display-message -p '#{pane_current_command}' | grep -iqE '(^|\/)vim$' && tmux send-keys 'C-\\') || tmux select-pane -l"
bind C-l send-keys 'C-l'

# [prefix-q] 展示窗口数字并选择跳转
bind-key q display-panes
# bind-key a last-pane # 绑什么好?

# 窗口大小调整
# resize panes using PREFIX H, J, K, L
bind H resize-pane -L 5
bind J resize-pane -D 5
bind K resize-pane -U 5
bind L resize-pane -R 5

# layout
# [prefix-space]
# bind-key + select-layout main-horizontal
# bind-key = select-layout main-vertical

# -------------------------------- pane end --------------------------------

# -------------------------------- move/copy/paste begin --------------------------------
# use vi mode
# vi is good  把 Tmux 设为 vi 模式，使其与 Vim 的操作更加接近
setw -g mode-keys vi

# [ 进入复制，space键开始选择问题  方向键移动， / 回车复制  / ] 黏贴
bind-key [ copy-mode
bind-key ] paste-buffer

# Copy and paste like in vim
# p 换成paste
unbind p
bind p paste-buffer


# the following vi-copy bindings match my vim settings
#   see https://github.com/gpakosz/.vim.git
# Setup 'v' to begin selection as in Vim
# bind -t vi-copy v begin-selection
bind-key -t vi-copy v begin-selection
bind -t vi-copy C-v rectangle-toggle
# bind -t vi-copy y copy-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
bind -t vi-copy Escape cancel
bind -t vi-copy H start-of-line
bind -t vi-copy L end-of-line
# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"


# for osx
# Enable native Mac OS X copy/paste  对于mac用户，可以复制到系统剪贴板
set-option -g default-command "/bin/bash -c 'which reattach-to-user-namespace >/dev/null && exec reattach-to-user-namespace $SHELL -l || exec $SHELL -l'"
# -------------------------------- move/copy/paste end --------------------------------

# -------------------------------- mouse begin --------------------------------
# mouse behavior
setw -g mode-mouse on
set -g mouse-select-pane on
set -g mouse-resize-pane on
# -------------------------------- mouse end --------------------------------

# -------------------------------- status line begin  --------------------------------
# 颜色
# set-option -g status-bg black
# set-option -g status-fg white

set -g status-bg '#4e4e4e'
set -g status-fg '#bbbbbb'
set -g status-left-fg '#bbbbbb'
set -g status-left-bg '#4e4e4e'
set -g status-right-fg '#bbbbbb'
set -g status-right-bg '#4e4e4e'

set -g status-left-length 90
set -g status-right-length 60

# 对齐方式
set-option -g status-justify centre

# 左下角
# set-option -g status-left '#[bg=black,fg=green][#[fg=cyan]#S#[fg=green]]'
set-option -g status-left "S-#S:W-#I:P-#P"

# 窗口列表
setw -g automatic-rename on
# set-window-option -g window-status-current-fg magenta
set-window-option -g window-status-format '#[dim]#I:#[dim]#W#[fg=grey,dim]'
set-window-option -g window-status-current-format '[#[fg=white,bold]#I#[fg=white,bold]:#[fg=white,bold]#W#[fg=dim]]'


# 右下角
set -g status-right '#[fg=green][#[fg=white]%m-%d %H:%M#[fg=green]]'


# -------------------------------- status line end --------------------------------
# -------------------------------- color begin --------------------------------

# pane border colors
set-option -g pane-active-border-fg '#55ff55'
set-option -g pane-border-fg '#555555'
# set -g pane-border-fg colour245
# set -g pane-active-border-fg colour39

# -------------------------------- color end --------------------------------

#--------------------------- base config end -------------------------------

